package servlet;

import dao.OrderDao;
import model.Order;
import model.Product;
import model.User;
import service.OrderService;
import service.ProductService;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "order_delete", urlPatterns = "/order_delete")
public class OrderDeleteServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       User user = (User) request.getSession().getAttribute("user");
        int orderId = Integer.parseInt(request.getParameter("id"));
        OrderService oService = new OrderService();
        ProductService pService = new ProductService();
        int productId = oService.getProductIdById(orderId);
        oService.delete(orderId);
        pService.delete(productId);

        //更新上下文中orderList的数据
        List<Order> orderList= oService.selectAllByReceiverId(user.getId());
        this.getServletContext().removeAttribute("orderList");
        this.getServletContext().setAttribute("orderList",orderList);

        //还未完成的orderlist
        List<Order> orderUnFinishedList= oService.selectUnFinishedByReceiverId(user.getId());
        this.getServletContext().removeAttribute("orderUnFinishedList");
        this.getServletContext().setAttribute("orderUnFinishedList",orderUnFinishedList);

        //更新goodsList数据
        List<Product> goodslist= pService.GetAllOnSaleProduct();
        this.getServletContext().removeAttribute("goodsList");
        this.getServletContext().setAttribute("goodsList",goodslist);

        request.getRequestDispatcher("/my_publish.jsp").forward(request, response);
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }


}